package com.org.zp.ctrl.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.entity.search.JobList;

public class OtherPositionActivity extends BaseActivity {

	private ListView listView;
	private PaginationAdapter adapter;
	private TextView title;
	private Button back_btn;
	private Button enable_btn;
	private ProgressDialog progressDialog;
	private List<JobList> newsItems = new ArrayList<JobList>();

	private String com_number = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_position);
		
		if (!(ActivityTaskManager.getInstance().containsName("OtherPositionActivity"))) {
			ActivityTaskManager.getInstance().putActivity("OtherPositionActivity",
					this);
		}

		title = (TextView) findViewById(R.id.headTitle);
		back_btn = (Button) findViewById(R.id.back);
		enable_btn = (Button) findViewById(R.id.top_right_button);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity("OtherPositionActivity");
			}
		});
		title.setText("该公司其他职位");
		enable_btn.setVisibility(View.INVISIBLE);
		progressDialog = ProgressDialog.show(this, "请等待...", "正在加载信息，请稍后...",true, true);
		initializeAdapter();
	}

	class QueryOtherPositionAsyncTask extends AsyncTask<String, Void, String> {
		
		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			
			
			map.put("act", "companyListOtherJobs");
			map.put("id", com_number);
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {
			
			progressDialog.dismiss();

			if (result == null || result.equals("")) {
				
			} else if (result.equals("timeout")) {
				
			} else {
				try {
					JSONObject json = new JSONObject(result);
					String succeed = json.getString("succeed");
					if (succeed.equals("000")) {
						
						String data = json.getString("dataList");
						JSONArray list = new JSONArray(data);
						for (int i = 0; i < list.length(); i++) {
							JobList info = new JobList();
							JSONObject log = list.getJSONObject(i);
							info.setJob_numer(log.getString("id"));
							info.setJob_title(log.getString("jobs_name"));
							newsItems.add(info);
						}

					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

				// We need notify the adapter that the data have been changed
				adapter.notifyDataSetChanged();
			}

		}
		

	}

	/**
	 * 初始化ListView的适配器
	 */
	private void initializeAdapter() {
		Intent intent = getIntent();
		com_number = intent.getStringExtra("Com_number");
		
		adapter = new PaginationAdapter(newsItems);
		listView = (ListView) findViewById(R.id.other_position_list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ActivityTaskManager.getInstance().removeActivity(
						"PositionActivity");
				Intent intent = new Intent(OtherPositionActivity.this,
						PositionActivity.class);
				intent.putExtra("Job_number", newsItems.get(position).getJob_numer());
				startActivity(intent);
				ActivityTaskManager.getInstance().removeActivity("OtherPositionActivity");
			}
		});
		new QueryOtherPositionAsyncTask().execute(URLHelper.mPrefix);
	}

	class PaginationAdapter extends BaseAdapter {

		List<JobList> newsItems;
		
		public PaginationAdapter(List<JobList> newsitems) {
			this.newsItems = newsitems;
		}

		@Override
		public int getCount() {
			return newsItems.size();
		}

		@Override
		public Object getItem(int position) {
			return newsItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			if (view == null) {
				view = getLayoutInflater().inflate(
						R.layout.other_position_items, null);
			}
			// 标题
			TextView tvTitle = (TextView) view.findViewById(R.id.newstitle);
			tvTitle.setText(newsItems.get(position).getJob_title());
			return view;
		}

	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity("OtherPositionActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
